package com.wlyuan.open.scanner.application.listener.runner;

import com.wlyuan.open.scanner.application.factory.IncrSyncHandler;
import com.wlyuan.open.scanner.application.factory.ScannerHandlerFactory;
import com.wlyuan.open.scanner.application.factory.TableNameHandlerFactory;
import com.wlyuan.open.scanner.application.handler.full.BaseScannerHandler;
import com.wlyuan.open.scanner.application.handler.incr.BaseIncrHandler;
import com.wlyuan.open.scanner.application.handler.table.BaseTableToIncrHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ScannerInitHandler implements InitializingBean {
    private final ApplicationContext context;

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("init scanner handler ...");
        ScannerHandlerFactory.initHandlers(Optional.ofNullable(context.getBeansOfType(BaseScannerHandler.class)).map(m -> m.values()).orElse(null));
        IncrSyncHandler.initHandler(Optional.ofNullable(context.getBeansOfType(BaseIncrHandler.class)).map(m -> m.values()).orElse(null));
        TableNameHandlerFactory.initHandlers(Optional.ofNullable(context.getBeansOfType(BaseTableToIncrHandler.class)).map(m -> m.values()).orElse(null));

    }
}
